export function jsonFormatString(data: any) {
  return JSON.stringify(data, null, 2);
}

export function jsonToCSV(jsonData: any[], head: string[], filename: string) {
  let str = `${head.join(',')}\n`;

  for (let i = 0; i < jsonData.length; i++) {
    const row = [];

    for (let item in jsonData[i]) {
      row.push(jsonData[i][item]);
    }

    str += `${row.join(',')}\n`;
  }
  //encodeURIComponent解决中文乱码
  let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);

  let link = document.createElement('a');
  link.href = uri;

  link.download = `${filename}.csv`;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
